package com.ssyt.ec.exam.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.ssyt.ec.common.PageModel;
import com.ssyt.ec.common.util.DBUtil;
import com.ssyt.ec.exam.entity.ExamPaperEntity;
import com.ssyt.ec.exam.entity.ExamRecordsEntity;

public class ExamRecordsService {
	public PageModel<ExamRecordsEntity> listExamRecords(String sort,
			String order, int page, int rows) {
		StringBuilder sb = new StringBuilder();
		sb.append("SELECT er.start_time,er.submit_time,er.get_point,er.is_pass, ");
		sb.append(" su.name AS sys_user_name,qe.name AS qes_exam_paper_name,qe.total_point AS qes_exam_paper_total_point ");
		sb.append(" FROM sys_user su,qes_exam_paper qe,exam_records er,exam_plan ep");
		sb.append(" WHERE er.user_id = su.id AND er.exam_plan_id = ep.id AND ep.exam_paper_id = qe.id");
		sb.append(" ORDER BY er.id ");
		sb.append(" LIMIT ?, ? ");

		return DBUtil.executeQueryByPage(ExamRecordsEntity.class,
				sb.toString(), (page - 1) * rows, rows);
	}

	public PageModel<ExamRecordsEntity> search(int page, int rows,
			String searchName, String papernameChoose) {
		List<Object> paramList = new ArrayList<>();
		StringBuilder sb = new StringBuilder();
		sb.append("SELECT er.start_time,er.submit_time,er.get_point,er.is_pass, ");
		sb.append(" su.name AS sys_user_name,qe.name AS qes_exam_paper_name,qe.total_point AS qes_exam_paper_total_point ");
		sb.append(" FROM sys_user su,qes_exam_paper qe,exam_records er,exam_plan ep");
		sb.append(" WHERE er.user_id = su.id AND er.exam_plan_id = ep.id AND ep.exam_paper_id = qe.id");
		if (StringUtils.isNotBlank(searchName)) {
			sb.append("AND su.name LIKE ? ");
			paramList.add("%" + searchName + "%");
		}
		sb.append(" ORDER BY er.id ");
		sb.append(" LIMIT ?,? ");
		paramList.add((page - 1) * rows);
		paramList.add(rows);
		return DBUtil.executeQueryByPage(ExamRecordsEntity.class,
				sb.toString(), paramList);
	}

	public List<ExamPaperEntity> listpapername() {
		String sql = "select distinct name from qes_exam_paper where state=1";
		return DBUtil.executeQuery(ExamPaperEntity.class, sql);
	}

}
